/**
 * 
 */
package at.ac.tuwien.swa075.telco.smsc.dao.memory;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import at.ac.tuwien.swa075.telco.smsc.dao.IMessageDAO;
import at.ac.tuwien.swa075.telco.smsc.dao.MessageStatus;
import at.ac.tuwien.swa075.telco.smsc.dao.MessageTO;

/**
 * @author vovmj01
 *
 */
public class MemoryMessageDAO implements IMessageDAO {
	
	private static List<MessageTO> messages = new ArrayList<MessageTO>();
	private static Map<String,List<MessageTO>> messagesByRecipient = new HashMap<String, List<MessageTO>>();
	private static Map<String,List<MessageTO>> messagesBySender = new HashMap<String, List<MessageTO>>();
	
	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.smsc.dao.memory.IMessageDAO#deleteMessage(java.lang.String)
	 */
	public boolean deleteMessage(String messageId) {
		MessageTO message = messages.get(Integer.parseInt(messageId));
		removeMessageByRecipient(message);
		removeMessageBySender(message);
		messages.remove(messageId);
		return true;
	}
	
	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.smsc.dao.memory.IMessageDAO#selectMessagesByRecipient(java.lang.String, java.lang.String)
	 */
	public List<MessageTO> selectMessagesByRecipient(String code, String number) {
		return messagesByRecipient.get(code + number);
	}
	
	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.smsc.dao.memory.IMessageDAO#selectMessagesBySender(java.lang.String, java.lang.String)
	 */
	public List<MessageTO> selectMessagesBySender(String code, String number) {
		return messagesBySender.get(code + number);
	}
	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.smsc.dao.memory.IMessageDAO#insertMessage(at.ac.tuwien.swa075.telco.smsc.dao.MessageTO)
	 */
	public String insertMessage(MessageTO message) {
		int messageId = messages.size() + 1;
		message.id = Integer.toString(messageId);
		messages.add(message);
		addMessageByRecipient(message);
		addMessageBySender(message);
		return message.id;
	}
	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.smsc.dao.memory.IMessageDAO#selectMessages(java.lang.String, java.lang.String, java.lang.String, java.lang.String, at.ac.tuwien.swa075.telco.smsc.dao.MessageStatus, java.sql.Timestamp, java.sql.Timestamp)
	 */
	public List<MessageTO> selectMessages(String senderCode, String senderNumber, String recipientCode, String recipientNumber, MessageStatus status, Timestamp from, Timestamp to) {
		// TODO: Implement method
		return null;
		
	}
	/* (non-Javadoc)
	 * @see at.ac.tuwien.swa075.telco.smsc.dao.memory.IMessageDAO#updateMessage(at.ac.tuwien.swa075.telco.smsc.dao.MessageTO)
	 */
	public boolean updateMessage(MessageTO message) {
		// nothing to do since we use in memory references.
		return true;
	}
	private void addMessageByRecipient(MessageTO message) {
		String key = message.recipientCode + message.recipientCode;
		List<MessageTO> foundMessages = messagesByRecipient.get(key);
		if (foundMessages == null) {
			foundMessages = new ArrayList<MessageTO>();
			messagesByRecipient.put(key, foundMessages);
		}
		foundMessages.add(message);
		
	}
	private void addMessageBySender(MessageTO message) {
		String key = message.senderCode + message.senderNumber;
		List<MessageTO> messages = messagesBySender.get(key);
		if (messages == null) {
			messages = new ArrayList<MessageTO>();
			messagesBySender.put(key, messages);
		}
		messages.add(message);
	}
	
	/**
	 * @param message
	 */
	private void removeMessageByRecipient(MessageTO message) {
		String key = message.recipientCode + message.recipientNumber;
		List<MessageTO> foundMessages = messagesByRecipient.get(key);
		foundMessages.remove(message);
		if (foundMessages.size() == 0) {
			messagesByRecipient.remove(key);
		}
	}
	/**
	 * @param message
	 */
	private void removeMessageBySender(MessageTO message) {
		String key = message.senderCode + message.senderNumber;
		List<MessageTO> foundMessages = messagesBySender.get(key);
		foundMessages.remove(message);
		if (foundMessages.size() == 0) {
			messagesBySender.remove(key);
		}
	}
}
